﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Com.CMS263.PO;
using NHibernate;
using NHibernate.Criterion;
using hr = System.Web.HttpRuntime;
using Com.CMS263.Caches;
namespace Com.CMS263.DAO
{
    public class ArtBaseDAO:BaseService
    {
        ArtCache ac = new ArtCache();
        string fileName = System.Configuration.ConfigurationSettings.AppSettings["cachepath"];
        public void Add(ArtBase ab)
        {


            DBHelper.TransSaveOrUpdate(ab);

            fileName += "Art\\Art" + ab.id + ".txt";
            ac.AddArt(ab);
        }
        public void Modify(ArtBase ab)
        {
            DBHelper.TransUpdate(ab);


            fileName += "Art\\Art" + ab.id + ".txt";
            ac.AddArt(ab);


        }
        public void Delete(int id)
        {
            DBHelper.TransDeleteByID("ArtBase", id);
            DBHelper.DeleteByHQL("delete from ExtValue where baseArtId=" + id + "");


        }
        public ArtBase GetArtById(int id)
        {

            return (ArtBase)DBHelper.GetObject(typeof(ArtBase), id);
        }
        public IList<ArtBase> GetArtBaseByTypeId(int typeId, int pageSize, int page)
        {
            IQuery iq = DBHelper.ExecuteHQL("from ArtBase where menuId=" + typeId + " order by orderNum asc");
            if (iq != null)
            {
                return iq.SetFirstResult((page - 1) * pageSize).SetMaxResults(pageSize).List<ArtBase>();
            }
            return null;
        }
        /// <summary>
        /// 得到查询结果
        /// </summary>
        /// <param name="typeId">父类编号</param>
        /// <param name="page">第几页</param>
        /// <param name="pageSize">每页条数</param>
        /// <param name="key">搜索的关键字</param>
        /// <returns></returns>
        public IList<ArtBase> GetResultByKey(int menuId, int page, int pageSize, string key)
        {
            ISession session = DBHelper.GetISession;
            if (string.IsNullOrEmpty(key))
            {
                return (IList<ArtBase>)session.CreateCriteria(typeof(ArtBase)).Add(Expression.Eq("menuId", menuId)).AddOrder(new Order("orderNum", true)).AddOrder(new Order("addDate", false)).SetFirstResult(pageSize * (page - 1)).SetMaxResults(pageSize).List<ArtBase>();
            }
            else
            {
                return (IList<ArtBase>)session.CreateCriteria(typeof(ArtBase)).Add(Expression.Like("title", "%" + key + "%")).Add(Expression.Eq("menuId", menuId)).AddOrder(new Order("orderNum", true)).AddOrder(new Order("addDate", false)).SetFirstResult(pageSize * (page - 1)).SetMaxResults(pageSize).List<ArtBase>();
            }
        }
        public IList<ArtBase> GetResultByMenuId(int menuId, int page, int pageSize)
        {
            ISession session = DBHelper.GetISession;
            try
            {


                return (IList<ArtBase>)session.CreateQuery("from ArtBase where menuId=" + menuId + " or menuId in (select id from ArtType where pid=" + menuId + ") or menuId in (select id from ArtType where pid in (select id from ArtType where pid=" + menuId + ")) order by orderNum asc,addDate desc ").SetFirstResult(pageSize * (page - 1)).SetMaxResults(pageSize).List<ArtBase>();

            }
            catch (Exception)
            {

                return null;
            }
        }

        public IList<ArtBase> GetResultByMenuIdClickCount(int menuId, int page, int pageSize)
        {
            ISession session = DBHelper.GetISession;
            try
            {


                return (IList<ArtBase>)session.CreateQuery("from ArtBase where menuId=" + menuId + " or menuId in (select id from ArtType where pid=" + menuId + ") or menuId in (select id from ArtType where pid in (select id from ArtType where pid=" + menuId + ")) order by isBest desc, orderNum asc,addDate desc ").SetFirstResult(pageSize * (page - 1)).SetMaxResults(pageSize).List<ArtBase>();

            }
            catch (Exception)
            {

                return null;
            }
        }
        public int GetResultByMenuId(int menuId)
        {
            ISession session = DBHelper.GetISession;
            try
            {


                return Convert.ToInt32(session.CreateQuery("select count(*) from ArtBase where menuId=" + menuId + " or menuId in (select id from ArtType where pid=" + menuId + ") or menuId in (select id from ArtType where pid in (select id from ArtType where pid=" + menuId + " ))").UniqueResult());

            }
            catch (Exception)
            {

                return 0;
            }
        }
        /// <summary>
        /// 得到根据搜索出的数据一共有多少条
        /// </summary>
        /// <param name="typeId">分类编号</param>
        /// <param name="page">页数</param>
        /// <param name="pageSize">一页几条</param>
        /// <param name="key">搜索的关键字</param>
        /// <returns></returns>
        public int GetResultTotalByKey(int menuId, string key)
        {
            ISession session = DBHelper.GetISession;
            if (string.IsNullOrEmpty(key))
            {
                return session.CreateQuery("from ArtBase where menuId=" + menuId).List().Count;
            }
            else
            {
                return session.CreateCriteria(typeof(ArtBase)).Add(Expression.Like("title", "%" + key + "%")).Add(Expression.Eq("menuId", menuId)).List().Count;
            }
        }
        /// <summary>
        /// 得到根据搜索出的数据一共有多少条
        /// </summary>
        /// <param name="typeId">分类编号</param>
        /// <param name="page">页数</param>
        /// <param name="pageSize">一页几条</param>
        /// <param name="key">搜索的关键字</param>
        /// <returns></returns>
        public IList<ArtBase> GetResultByKey(int menuId, string key)
        {
            ISession session = DBHelper.GetISession;
            if (string.IsNullOrEmpty(key))
            {
                return session.CreateQuery("from ArtBase where menuId=" + menuId).List<ArtBase>();
            }
            else
            {
                return session.CreateCriteria(typeof(ArtBase)).Add(Expression.Like("title", "%" + key + "%")).Add(Expression.Eq("menuId", menuId)).List<ArtBase>();
            }
        }

        public ArtBase GetArtBaseByMenuId(int menuId)
        {
            object obj = DBHelper.ExecuteHQL("from ArtBase where menuId=" + menuId + "").UniqueResult();
            if (obj == null)
            {
                return null;
            }
            else
            {
                return (ArtBase)obj;
            }
        }

        public IList<ArtBase> GetArtBaseListByMenuId(int menuId)
        {
            IQuery iq = DBHelper.ExecuteHQL("from ArtBase where menuId=" + menuId + " order by id asc");
            if (iq != null)
            {
                return iq.List<ArtBase>();
            }
            else
            {
                return null;
            }
        }

        /// <summary>
        /// 获取当前的排序字段
        /// </summary>
        /// <param name="menuId"></param>
        /// <returns></returns>
        public int GetMaxArtOrderNumByMenuId(int menuId)
        {
            IQuery iq = DBHelper.ExecuteHQL("from ArtBase where menuId=" + menuId + " order by orderNum desc");
            if (iq != null)
            {
                try
                {
                    return iq.SetFirstResult(0).SetMaxResults(1).List<ArtBase>()[0].orderNum;
                }
                catch
                {

                    return 0;
                }
            }
            else
            {
                return 0;
            }
        }


        /// <summary>
        /// 根据关键字和menuids串查询信息
        /// </summary>
        /// <param name="menuIds"></param>
        /// <param name="key"></param>
        /// <param name="page"></param>
        /// <param name="pagesize"></param>
        /// <returns></returns>
        public IList<ArtBase> GetResultByKeyAndMenuIds(string menuIds, string key, int page, int pagesize)
        {
            ISession session = DBHelper.GetISession;
            string hql = "from ArtBase where 1=1";
            if (string.IsNullOrEmpty(key))
            {
                hql += " and menuId in (" + menuIds + ")";

            }
            else
            {
                hql += " and menuId in (" + menuIds + ") and title like '%" + key + "%'";
            }
            try
            {
                return session.CreateQuery(hql).SetFirstResult((page - 1) * pagesize).SetMaxResults(pagesize).List<ArtBase>();
            }
            catch (Exception)
            {
                return null;
            }
        }
        /// <summary>
        /// 根据关键字和menuids串查询信息个数
        /// </summary>
        /// <param name="menuIds"></param>
        /// <param name="key"></param>
        /// <param name="page"></param>
        /// <param name="pagesize"></param>
        /// <returns></returns>
        public int GetResultByKeyAndMenuIds(string menuIds, string key)
        {
            ISession session = DBHelper.GetISession;
            string hql = "from ArtBase where 1=1";
            if (string.IsNullOrEmpty(key))
            {
                hql += " and menuId in (" + menuIds + ")";

            }
            else
            {
                hql += " and menuId in (" + menuIds + ") and title like '%" + key + "%'";
            }
            try
            {
                return session.CreateQuery(hql).List().Count; ;
            }
            catch (Exception)
            {
                return 0;
            }
        }
        //*************************************华丽的分割****************************************//
        /// <summary>
        /// 根据sql查询集合结果
        /// </summary>
        /// <param name="hql"></param>
        /// <param name="page"></param>
        /// <param name="pagesize"></param>
        /// <returns></returns>
        public IList<ArtBase> GetListBySql(string hql, int page, int pagesize)
        {
            ISession session = DBHelper.GetISession;
            try
            {
                if (page == -1)
                {
                    return session.CreateQuery(hql).List<ArtBase>();
                }
                else
                {
                    return session.CreateQuery(hql).SetFirstResult((page - 1) * pagesize).SetMaxResults(pagesize).List<ArtBase>();
                }
            }
            catch
            {
                return null;
            }
        }
        /// <summary>
        /// 根据sql查询条目数
        /// </summary>
        /// <param name="hql"></param>
        /// <returns></returns>
        public int GetCountBySql(string hql)
        {
            ISession session = DBHelper.GetISession;
            try
            {
                return session.CreateQuery(hql).List<ArtBase>().Count;
            }
            catch
            {
                return 0;
            }
        }
        public IList<ArtBase> GetListBySortId(int menuid, string sortstate, string key, int page, int pagesize)
        {
            string hql = " from ArtBase where 1=1 ";
            if (string.IsNullOrEmpty(key))
            {
                hql += " and menuId =" + menuid;
            }
            else
            {
                hql += " and menuId =" + menuid + " and title like '%" + key + "%'";
            }
            hql += " order by isBest desc,orderNum " + sortstate + ",addDate desc";
            return GetListBySql(hql, page, pagesize);
        }
        public int GetListBySortId(int menuid, string key)
        {
            string hql = " from ArtBase where 1=1 ";
            if (string.IsNullOrEmpty(key))
            {
                hql += " and menuId =" + menuid;
            }
            else
            {
                hql += " and menuId =" + menuid + " and title like '%" + key + "%'";
            }
            return GetCountBySql(hql);
        }
        public IList<ArtBase> GetListBySortIds(string menuid, string sortstate, string key, int page, int pagesize)
        {
            string hql = " from ArtBase where 1=1 ";
            if (string.IsNullOrEmpty(key))
            {
                hql += " and menuId in (" + menuid + ")";
            }
            else
            {
                hql += " and menuId =" + menuid + " and title like '%" + key + "%'";
            }
            hql += " order by isBest desc,orderNum " + sortstate + ",addDate desc";
            return GetListBySql(hql, page, pagesize);
        }
        public int GetListBySortIds(string menuid, string key)
        {
            string hql = " from ArtBase where 1=1 ";
            if (string.IsNullOrEmpty(key))
            {
                hql += " and menuId in (" + menuid + ")";
            }
            else
            {
                hql += " and menuId =" + menuid + " and title like '%" + key + "%'";
            }
            return GetCountBySql(hql);
        }
        //*************************************华丽的分割****************************************//
        public IList<ArtBase> GetResultByKA(int artId, string key, int page, int pageSize,int pid)
        {
            ISession session = DBHelper.GetISession;
            string hql = "from ArtBase where 1=1 and typeId="+pid;
            if (string.IsNullOrEmpty(key))
            {
                hql += " and artId in (" + artId + ")";

            }
            else
            {
                hql += " and artId in (" + artId + ") and title like '%" + key + "%'";
            }
            try
            {
                return session.CreateQuery(hql).SetFirstResult((page - 1) * pageSize).SetMaxResults(pageSize).List<ArtBase>();
            }
            catch (Exception)
            {
                return null;
            }
        }

        public int GetCountByArtId(int artId, string key,int pid)
        {


            ISession session = DBHelper.GetISession;
            return int.Parse(session.CreateQuery("select count(id) from  ArtBase where  typeId="+pid+" and artId=" + artId + " and title like '%" + key + "%'").UniqueResult().ToString());
        }

        public IList<ArtBase> GetArtBaseListByArtId(int artId, string key, int page, int pageSize)
        {
            ISession session = DBHelper.GetISession;
            string hql = "from ArtBase where 1=1";
            if (string.IsNullOrEmpty(key))
            {
                hql += " and artId in (" + artId + ")";

            }
            else
            {
                hql += " and artId in (" + artId + ") and title like '%" + key + "%'";
            }
            try
            {
                return session.CreateQuery(hql).SetFirstResult((page - 1) * pageSize).SetMaxResults(pageSize).List<ArtBase>();
            }
            catch (Exception)
            {
                return null;
            }
        }
        public int GetPrev(int id, int menuid, int parentId, string sort)
        {
            string hql = " from ArtBase where 1=1";
            if (menuid == 0)
            {
                hql += " and menuId in(select id from ArtType where pid = " + parentId + ")";
            }
            else
            {
                hql += " and menuId =" + menuid;
            }
            if (sort.ToLower() == "desc")
            {
                hql += " and orderNum<" + id + " order by orderNum Desc";
            }
            else
            {
                hql += " and orderNum>" + id + " order by orderNum asc";
            }
            try
            {
                return FindList<ArtBase>(hql, null, 0, 1).First().id;
            }
            catch (Exception)
            {

                return 0;
            }
        }
    }
}
